package com.fick.hibernate.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.fick.hibernate.model.Media;

/**
 * A data access object (DAO) providing persistence and search support for Media
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.fick.Media.nrgl.Media
 * @author MyEclipse Persistence Tools
 */

public class MediaDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(MediaDAO.class);
	// property constants
	public static final String MEDIA_NAME = "mediaName";
	public static final String MEDIA_URL = "mediaUrl";
	public static final String MEDIA_TYPE = "mediaType";
	public static final String MEDIA_PLAYTIME = "mediaPlaytime";
	public static final String MEDIA_DESCRIPTION = "mediaDescription";
	public static final String MEDIA_ICO = "mediaIco";
	public static final String MEDIA_ORDER = "mediaOrder";
	public static final String MEDIA_STATUS = "mediaStatus";
	public static final String MEDIA_VISTCOUNT = "mediaVistcount";
	public static final String ADDUSER = "adduser";

	public void attachClean(Media instance) {
		log.debug("attaching clean Media instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachDirty(Media instance) {
		log.debug("attaching dirty Media instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Media persistentInstance) {
		log.debug("deleting Media instance");
		try {
			super.delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Media instances");
		try {
			String queryString = "from Media";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/**
	 * 
	 * @param currPage
	 * @param pageSize
	 * @param str
	 * @return
	 */
	public List findAll(String str) {
		log.debug("finding some page Media instances");
		try {
			String queryString = "from Media " + str;
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find some page failed", re);
			throw re;
		}
	}

	public List findByAdduser(Object adduser) {
		return findByProperty(ADDUSER, adduser);
	}

	public List findByExample(Media instance) {
		log.debug("finding Media instance by example");
		try {
			List results = getSession().createCriteria("com.fick.hibernate.model.Media").add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public Media findById(java.lang.String id) {
		log.debug("getting Media instance with id: " + id);
		try {
			Media instance = (Media) getSession().get("com.fick.hibernate.model.Media", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByMediaDescription(Object mediaDescription) {
		return findByProperty(MEDIA_DESCRIPTION, mediaDescription);
	}

	public List findByMediaIco(Object mediaIco) {
		return findByProperty(MEDIA_ICO, mediaIco);
	}

	public List findByMediaName(Object mediaName) {
		return findByProperty(MEDIA_NAME, mediaName);
	}

	public List findByMediaOrder(Object mediaOrder) {
		return findByProperty(MEDIA_ORDER, mediaOrder);
	}

	public List findByMediaPlaytime(Object mediaPlaytime) {
		return findByProperty(MEDIA_PLAYTIME, mediaPlaytime);
	}

	public List findByMediaStatus(Object mediaStatus) {
		return findByProperty(MEDIA_STATUS, mediaStatus);
	}

	public List findByMediaType(Object mediaType) {
		return findByProperty(MEDIA_TYPE, mediaType);
	}

	public List findByMediaUrl(Object mediaUrl) {
		return findByProperty(MEDIA_URL, mediaUrl);
	}

	public List findByMediaVistcount(Object mediaVistcount) {
		return findByProperty(MEDIA_VISTCOUNT, mediaVistcount);
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Media instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Media as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/**
	 * 分页
	 * 
	 * @param currPage
	 * @param pageSize
	 * @param str
	 * @return
	 */
	public List findPage(int currPage, int pageSize, String str) {
		log.debug("finding some page Media instances");
		try {
			String queryString = "from Media " + str;

			Query queryObject = getSession().createQuery(queryString);

			queryObject.setFirstResult((currPage - 1) * pageSize);
			queryObject.setMaxResults(pageSize);

			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find some page failed", re);
			throw re;
		}
	}

	public Media merge(Media detachedInstance) {
		log.debug("merging Media instance");
		try {
			Media result = (Media) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void save(Media transientInstance) {
		log.debug("saving Media instance");
		try {
			super.save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
}